package enums;

import java.lang.reflect.Method;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

/**
 * Describe
 *
 * @author maguangming
 * @since 2022/8/31 21:40
 */
public enum DemoEnum {
    UNSET(-1, "UNSET"),
    INIT(0, "INIT"),
    FIRST(1, "FIRST"),
    SECOND(2, "SECOND"),
    ;

    private Integer code;

    private String desc;

    public Integer getCode() {
        return code;
    }

    DemoEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "DemoEnum-" + code + "-" + desc;
    }

    public static void main(String[] args) {
        System.out.println(UNSET);
        System.out.println("-----");

        // 无法实例化枚举类
        // final DemoEnum anEnum = new DemoEnum(99, "99");

        for (DemoEnum value : DemoEnum.values()) {
            System.out.println(value);
        }
        System.out.println("-----");

        Set<String> analyseEnum = Reflection.analyse(Enum.class);
        System.out.println("Enum method count:" + analyseEnum.size());

        Set<String> analyseDemoEnum = Reflection.analyse(DemoEnum.class);
        System.out.println("DemoEnum method count:" + analyseDemoEnum.size());

        Enum<DemoEnum> aEnum = DemoEnum.FIRST;
        System.out.println(aEnum);
        System.out.println(aEnum.compareTo(DemoEnum.SECOND));
        for (Enum enumConstant : aEnum.getClass().getEnumConstants()) {
            System.out.println(enumConstant);
        }

        final Random random = new Random(92);




    }

    public static class Reflection {

        public static Set<String> analyse(Class<?> enumClass) {

            System.out.println("analyse " + enumClass);
            System.out.println("superclass:" + enumClass.getSuperclass());
            Set<String> methods = new TreeSet<>();
            System.out.println("Methods:");
            for (Method method : enumClass.getMethods()) {
                methods.add(method.getName());
                System.out.println(method);
            }

            System.out.println("-----");
            return methods;
        }

    }
}


